export let treeOptions = [
    {
        value: '001',
        label: '分类一',
        children: [
            {
                value: '001001',
                label: '分类一1',
                children: [
                    {
                        value: '001001001',
                        label: '分类一1(1)',
                        children: [
                            {
                                value: '001001001001',
                                label: '分类一1(1)1',
                                children: null
                            },
                            {
                                value: '001001001002',
                                label: '分类一1(1)2',
                                children: null
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        value: '002',
        label: '分类二',
        children: [
            {
                value: '002002',
                label: '分类二1',
                children: [
                    {
                        value: '002002002',
                        label: '分类二1(1)',
                        children: [
                            {
                                value: '002002002002',
                                label: '分类二1(1)1',
                                children: null
                            },
                            {
                                value: '002002002002',
                                label: '分类二1(1)2',
                                children: null
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

export const getTreeData = (opt) => {
    let outPut = [];
    let id = opt.id;

    if(opt.type === 'id') {
        const getNodesById = (parents, id, isRoot=false) => {
            let targetArr = [];
            if(isRoot) {
                targetArr = parents;
            } else if(parents && parents.length) {
                for(let i=0; i<parents.length; i++) {
                    let item = parents[i];
                    if(item.value === id) {
                        targetArr = item.children;
                        break;
                    }else {
                        if(targetArr&&targetArr.length) {
                            return targetArr;
                        }
                        targetArr = getNodesById(item.children, id, false);
                    }
                }
            }
            return targetArr;
        }
        outPut = getNodesById(treeOptions, id, opt.node.root);
    }

    return (outPut||[]).map(item => {
        return {
            value: item.value,
            label: item.label,
            leaf: !item.children
        }
    });
}